package com.ht.right.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.ht.common.entity.PageInfo;
import com.ht.right.entity.User;
import com.ht.right.service.UserService;
import com.ht.right.util.CommonUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private UserService userService;
	private User user;
	
	public String loginOut(){
		//清空session
		ActionContext.getContext().getSession().clear();
		//使HttpSession失效
		ServletActionContext.getRequest().getSession().invalidate();
		return "success";
	}
	
	public String findUsersJson(){
		HttpServletRequest request = ServletActionContext.getRequest();
		int pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
		int pageSize = Integer.parseInt(request.getParameter("pageSize"));
		String sortField = request.getParameter("sortField");
		String sortOrder = request.getParameter("sortOrder");
		String deptId = request.getParameter("deptId");
		PageInfo pageInfo = new PageInfo(pageIndex, pageSize, sortField,sortOrder);
		CommonUtil.toJsonStr(ServletActionContext.getResponse(),userService.findListByDeptIdForPage(deptId,user, pageInfo));
		return null;
	}
	
	public String findUsersByDeptId(){
		CommonUtil.toJsonStr(ServletActionContext.getResponse(),userService.findListByDeptId(user));
		return null;
	}
	
	
	public String findUserById(){
		String userId = ServletActionContext.getRequest().getParameter("userId");
		user = userService.findUserById(userId);
		return SUCCESS;
	}
	
	public String updateUser(){
		String html =null;
		try{
			HttpServletRequest request = ServletActionContext.getRequest();
			String sameLevelNodeId = request.getParameter("sameLevelNodeId");
			userService.updateUser(user,sameLevelNodeId);
			html = "<script>parent.LoadUserInfo();parent.goCloseDlg(\"oWinDlg\");</script>";
		}catch(Exception e){
			html = "<script>alert('修改用户失败！');parent.goCloseDlg(\"oWinDlg\");</script>";
			e.printStackTrace();
		}
		CommonUtil.toString(ServletActionContext.getResponse(), html);
		return null;
		
	}
	
	public void deleteUsers(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String userIds = request.getParameter("userIds");
		userService.deleteUsersByIds(userIds);
	}
	
	public UserService getUserService() {
		return userService;
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}

	
}
